---@class BaseBuffEffect:IBuffEffect
BaseBuffEffect = ClientFight.CreateClass("BaseBuffEffect",nil,ClientFight.IBuffEffect)
function BaseBuffEffect:ctor()
    local buffId = 0
    local className = self.__cname
    if string.startsWith(className,"BuffEffect") then
        buffId = string.gsub(className,"BuffEffect","")
        buffId = tonumber(buffId)
    end
    BuffEffectManager.putBuffEffect(buffId,self)
end
---@param fight Fighter
---@param buff FighterBuff
function BaseBuffEffect:add(fight, buff,useAttribute)
    if useAttribute == nil then
        useAttribute = true
    end
    local effect = 1
    local flag1
    local buffBean = buff.buffBean
    if useAttribute then
        local flag = self:addAttribute(buff)
        flag1 = flag and 1 or 0
        effect = bit.bor(effect,flag1)
    end
    if(buffBean.f_BuffSpecialEffect ~= nil) then
        for k,state in pairs(buffBean.f_BuffSpecialEffect) do
            buff.state.state = bit.bor(buff.state.state,  bit.lshift(1,(state-1)))
        end
        effect = 1
    end
    flag1 = self:addFlag(fight,buff) and 1 or 0
    effect = bit.bor(effect,flag1)

    if effect == 1 then
        SkillRoleMgr.savePathFile("[" .. fight.frame  .. "][EFFECT]添加BUFF效果" .. buff.modelId);
        --XXX 魅惑需要重置攻击目标,这里可能需要考虑其他效果
        if FighterStateEnum.is(buff.state.state,FighterStateEnum.CHARM) then
            buff.target.resetPrepareTarget = true
        end
    end
    return IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE

end
---@param buff FighterBuff
function BaseBuffEffect:addAttribute(buff)
    local effect = false
    local buffBean = buff.buffBean
    if buffBean.f_Atribute ~= nil then
        for k,value in pairs(buffBean.f_Atribute) do
            buff.attributes.attributes[value[1]] = math.floor(value[2]*buff.overlay)
        end
        effect = true
    end
    return effect
end
---@param fight Fighter
---@param buff FighterBuff
function BaseBuffEffect:addFlag(fight,buff)
    local buffBean = buff.buffBean
    local effect = false
    if(buffBean.f_Tag ~= nil) then
        for k,sign in pairs(buffBean.f_Tag) do
            buff.sign.signs[sign]= true
            SkillRoleMgr.savePathFile(string.format("[%s][Buff]Fighter %s Buff%s 增加标记%s",
                    fight.frame, buff.target.fighterId, buff.modelId, sign
            ));
        end
        effect = true
    end
    return effect
end
---@param fight Fight
---@param buff FighterBuff
function BaseBuffEffect:action(fight, buff, ...)
    local buffBean = buff.buffBean
    local parameters = {...}
    if buffBean.f_AddBuff ~= nil then
        SkillRoleMgr.savePathFile("[" .. fight.frame  .. "][EFFECT]" .. buff.target.fighterId  .. "触发并增加其他BUFF"
                .. buff.modelId);
        for k,buffInfo in pairs(buffBean.f_AddBuff) do
            ---@type T_buffBean
            local addBuffBean = BuffManager.getBuffBean(buffInfo[1],buff.source)
            local continue = false
            if addBuffBean == nil then
                continue = true
            end
            if not continue then
                local target
                if #parameters > 0 and  type(parameters[1]) =="table" and parameters[1].__cname == Fighter.__cname then
                    target = parameters[1]
                end
                if target == nil or addBuffBean.f_Target == BuffTargetTypeEnum.PARENT then
                    target = buff.target
                end
                BuffManager.addBuff(fight,buff.skill,buff.source,target,false,buffInfo[1],buffInfo[2],buff.addParameters)
            end
        end
    end
    return IBuffEffect.SUCCESS_ATTRI_NO_CHANGE
end
---@param fight Fight
---@param buff FighterBuff
function BaseBuffEffect:remove( fight, buff,force,...)
    --解除魅惑时也需要重置攻击目标
    if FighterStateEnum.is(buff.state.state,FighterStateEnum.CHARM) then
        buff.target.resetPrepareTarget = true
    end
    local parameters = {...}
    local buffBean = buff.buffBean
    if buffBean.f_RemoveBuffAdd ~= nil then
        SkillRoleMgr.savePathFile( "[" .. fight.frame  .. "][EFFECT]" .. buff.target..fighterId  .. "触发并增加其他BUFF"
                .. buff.modelId);
        for k,buffInfo in pairs(buffBean.f_RemoveBuffAdd) do
            local addBuffBean = BuffManager.getBuffBean(buffInfo[1],buff.source)
            local continue = false
            if addBuffBean == nil then
                continue = true
            end
            if not continue then
                local target
                if #parameters > 0 and parameters[1].__cname == Fighter.__cname then
                    target = parameters[1]
                end
                if target == nil or addBuffBean.f_Target == BuffTargetTypeEnum.PARENT then
                    target = buff.target
                end
                BuffManager.addBuff(fight,buff.skill,buff.source,target,false,buffInfo[1],buffInfo[2],buff.addParameters)
            end
        end
    end
    return IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE
end
---触发技能 buff触发的技能 父技能是一个新对象
---@param fight Fight
---@param fighter Fighter
---@param target Fighter
---@param flyBegin Fighter
function BaseBuffEffect:triggerSkill( fight,  fighter, target, triggerSkill,flyBegin)
    ---@type T_skillBean
    local skillBean = SkillManager.getSkillBean(triggerSkill,fighter)
    if skillBean == nil then
        return false
    end
    -- 如果非分离技能，打断移动
    if (skillBean.f_SkillOut ~= 1) then
        if(fighter.moveTarget ~= nil or fighter.moveDirection ~= 0 or fighter.movePosition ~= nil) then
            --停止移动
            fighter.moveTarget = nil
            fighter.moveDirection = 0
            fighter.movePosition = nil
            ReportManager.setMoveEndToJsonString(fight, fighter, fighter.moveAction)
            fighter.moveAction = nil
            SkillRoleMgr.savePathFile( "[" .. fight.frame  .. "][STOP]Fighter " .. fighter..fighterId  .. " 停止移动"
                   ..  getJsonStr(fighter.position));
        end
    end

    local parentSkill = FightUsingSkill.New()
    parentSkill.fight = fight
    parentSkill.fighter = fighter
    parentSkill.target = target
    parentSkill.flyBegin = flyBegin
    parentSkill.position = Vector2.New(fighter.position.x,fighter.position.y)

    --触发技能
    return SkillManager.triggerSkill(parentSkill, target, triggerSkill);
end
---action触发是否是由伤害 前/后   被伤害 前/后  触发的,
---@param buff FighterBuff
function BaseBuffEffect:isDamageActionTrigger(buff)
    local actionTrigger = buff.buffBean.f_BuffActionTrigger
    return actionTrigger == FightEventEnum.BEFORE_DAMAGE or actionTrigger == FightEventEnum.BEFORE_BE_DAMAGE
    or actionTrigger == FightEventEnum.AFTER_DAMAGE or actionTrigger == FightEventEnum.AFTER_BE_DAMAGE
end